/**
 * $Id: DateUtil.java 6 2011-09-14 00:18:07Z adriftrock@gmail.com $
 */

package com.swehr.utils;

import com.hp.hpl.jena.rdf.model.Literal;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Rock Huang
 * @version 0.1
 */
public class DateUtil {
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    private DateUtil() {
    }

    public static Date parse(String dateString, String format) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(dateString);
    }

    public static Date parse(String dateString) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
        return dateFormat.parse(dateString);
    }

    public static String format(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
        return dateFormat.format(date);
    }

    public static String format() {
        return format(new Date());
    }

    public static Date parse(Literal literal) throws ParseException {
        if (literal == null) return null;
        String s = literal.getString();
        int p = s.indexOf("^");
        if (p != -1) {
            s = s.substring(0, s.indexOf("^"));
        }
        return parse(s, DATE_FORMAT);
    }

}
